﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;
using System.Collections;

namespace PeisongWeb.admin
{
    public partial class newsEdit : adminPage
    {
        protected int newsid
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["newsid"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["newsid"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("AC", 1))
                {
                    drpcssBind();
                    if (Request.QueryString["news"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["news"]))
                        {
                            newsid = Convert.ToInt32(Request.QueryString["news"]);
                        }
                    }
                    if (newsid != 0)
                        getDetails();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
                
            }
        }
        private void drpcssBind()
        {
            DataSet ds = new DataSet();
            ArrayList csslist = new ArrayList();
            ds.ReadXml(MapPath("~/app_common/xmls/NewsTitleCss.xml"));
            DataTable dt = new DataTable();
            DataRow[] dr = new DataRow[100];
            dt = ds.Tables[0];
            dr = dt.Select();
            for (int i = 0; i < dr.Length; i++)
            {
                csslist.Add(dr[i]["descript"].ToString());
            }
            drp_css.DataSource = csslist;
            drp_css.DataBind();
        }
        private void getDetails()
        {
            NewsEntity _news = logic.news.getById(newsid);
            txt_title.Text = _news.Title;
            txt_subtitle.Text = _news.Subtitle;
            drp_css.SelectedIndex = drp_css.Items.IndexOf(drp_css.Items.FindByValue(_news.TitleCss));
            txt_source.Text = _news.Source;
            txt_context.Text = _news.Context;
            cbx_isTop.Checked = _news.IsTop;
            cbx_isShow.Checked = _news.IsShow;
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            NewsEntity _news = new NewsEntity();
            _news.Newsid = newsid;
            _news.Type = "2"; 
            _news.Title = txt_title.Text;
            _news.Subtitle = txt_subtitle.Text;
            _news.TitleCss = drp_css.SelectedValue.ToString();
            _news.Source = txt_source.Text;
            _news.Context = txt_context.Text;
            _news.IsTop = cbx_isTop.Checked;
            _news.IsShow = cbx_isShow.Checked;
            _news.AdminId = logic.sysAdmin.AdminID;
            if (newsid != 0)
            {
                logic.news.update(_news);
            }
            else
            {
                logic.news.insert(_news);
            }
            Library.Script.ClientMsgUrl("保存成功！", "newsList.aspx");
        }
    }
}
